package com.unbox.admin.controller;
import com.unbox.admin.entity.Role;
import com.unbox.admin.entity.UnboxUser;
import com.unbox.admin.entity.UserRoles;
import com.unbox.admin.fegin.UnBoxUserFegin;
import com.unbox.admin.service.IUnboxUserService;
import com.unbox.admin.service.IUserRolesService;
import com.unbox.common.response.UnboxResponse;
import com.unbox.common.table.UnBoxPage;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "用户管理接口api")
@RestController
public class UserController implements UnBoxUserFegin {

    @Autowired
    IUnboxUserService unboxUserService;

    @Autowired
    IUserRolesService userRolesService;

    /**
     * 分页查询
     * @param user
     * @return
     */
    @ApiOperation(value = "分页查询列表")
    @Override
    public UnboxResponse<UnBoxPage> getPageList(UnboxUser user, Integer pageNo, Integer pageSize) {
        UnBoxPage<List<UnboxUser>> page = unboxUserService.getPageList(user,pageNo, pageSize);
        List<UnboxUser> unboxUsers = page.getDatas();
        List<Integer> userIds = unboxUsers.stream()
                .map(unboxUser -> unboxUser.getId())
                .collect(Collectors.toList());
        if (userIds.size() > 0) {
            List<UserRoles> roles = userRolesService.getUserIdsList(userIds);
            unboxUsers.forEach(unboxUser -> {
                unboxUser.setRoleList(roles
                        .stream()
                        .filter(userRoles -> userRoles.getUserId().equals(unboxUser.getId()))
                        .map(userRoles -> {
                            Role role = new Role();
                            role.setId(userRoles.getRoleId());
                            return role;
                        }).collect(Collectors.toList()));
            });
        }
        return new UnboxResponse<>(page);
    }

    /**
     * 详情
     * @param user
     * @return
     */
    @ApiOperation(value = "查询明细")
    @Override
    public UnboxResponse<UnboxUser> info(UnboxUser user) {
        UnboxUser unboxUser = unboxUserService.get(user);
        return new UnboxResponse<>(unboxUser);
    }

    /**
     * 通过id数据查询数据
     * @param ids
     * @return
     */
    @ApiOperation(value = "通过ids进行in查询")
    @Override
    public UnboxResponse<List<UnboxUser>> idsUnboxUsers(List<Integer> ids) {
        List<UnboxUser> unboxUsers = unboxUserService.getIdsList(ids);
        return new UnboxResponse<>(unboxUsers);
    }

    /**
     * 保存数据
     * @param user
     * @return
     */
    @ApiOperation(value = "保存数据")
    @Override
    @GlobalTransactional
    public UnboxResponse<Boolean> save(UnboxUser user) {
        UnboxUser unboxUser = new UnboxUser();
        unboxUser.setUserName(user.getUserName());
        if (!ObjectUtils.isEmpty(unboxUserService.get(unboxUser))) {
            return new UnboxResponse
                    .Builder<Boolean>()
                    .setBody(false)
                    .businessFail("用户已经存在")
                    .build();
        }
        Boolean flg = unboxUserService.save(user);
        return new UnboxResponse<>(flg);
    }

    /**
     * 批量添加
     * @param users
     * @return
     */
    @ApiOperation(value = "批量保存数据")
    @Override
    public UnboxResponse<Boolean> batchSave(List<UnboxUser> users) {
        Boolean flg = unboxUserService.saveBatch(users);
        return new UnboxResponse<>(flg);
    }

    /**
     * 修改数据
     * @param user
     * @return
     */
    @ApiOperation(value = "修改数据")
    @Override
    public UnboxResponse<Boolean> update(UnboxUser user) {
        Boolean flg = unboxUserService.update(user);
        return new UnboxResponse<>(flg);
    }

    /**
     * 批量修改
     * @param users
     * @return
     */
    @ApiOperation(value = "批量修改数据")
    @Override
    public UnboxResponse<Boolean> batchUpdate(List<UnboxUser> users) {
        Boolean flg = unboxUserService.updateBatchById(users);
        return new UnboxResponse<>(flg);
    }

    /**
     * 删除数据
     * @param user
     * @return
     */
    @ApiOperation(value = "删除数据")
    @Override
    public UnboxResponse<Boolean> delete(UnboxUser user) {
        Boolean flg = unboxUserService.remove(user);
        return new UnboxResponse<>(flg);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @Override
    public UnboxResponse<Boolean> deleteBatch(List<Integer> ids) {
        Boolean flg = unboxUserService.removeByIds(ids);
        return new UnboxResponse<>(flg);
    }
}

